<?php
namespace Home\Model;
use  Think\Model;

/* 访问控制 */
defined('IN_ECTOUCH') or die('Deny Access');
class PaymentModel extends Model{

    protected $autoCheckFields =false;
  /**
     * 修改订单的支付状态
     *
     * @access  public
     * @param   string  $log_id     支付编号
     * @param   integer $pay_status 状态
     * @param   string  $note       备注
     * @return  void
     */
    function order_paid($log_id, $pay_status =0, $note = '') {

        /* 取得支付编号 */
        $log_id = intval($log_id);
        if ($log_id > 0) {
            /* 取得要修改的支付记录信息 */
            $sql = "SELECT * FROM " .C('DB_PREFIX') .
                    "pay_log WHERE discarded_status=0 and log_id = '$log_id'";
            $list =M()->query($sql);


            $pay_log=$list[0];
            if ($pay_log && $pay_log['is_paid'] == 0) {
                /* 修改此次支付操作的状态为已付款 */
                $data['is_paid']=1;
                M('pay_log')->where("log_id = '$log_id'")->save($data);


                /* 根据记录类型做相应处理 */
                if ($pay_log['order_type'] == 0) {
                    /* 取得订单信息 */
                    $sql = 'SELECT user_id,shipping_fee,mshop_id,order_amount,order_id, user_id, order_sn, consignee, address, mobile, shipping_id, extension_code, extension_id, goods_amount ' .
                            'FROM ' . C('DB_PREFIX') .
                            "order_info WHERE order_id = '$pay_log[order_id]'";
                    $list =M()->query($sql);
                    $order =$list[0];
                    $order_id = $order['order_id'];
                    $order_sn = $order['order_sn'];
                    $user_id = $order['user_id'];

                    /* 修改订单状态为已付款 */
                    $sql = 'UPDATE ' . C('DB_PREFIX') .
                            "order_info SET order_status = '1', " .
                            " confirm_time = '" . gmtime() . "', " .
                            " pay_status = '$pay_status', " .
                            " pay_time = '" . gmtime() . "', " .
                            " money_paid = order_amount," .
                            " order_amount = 0 " .
                            "WHERE order_id = '$order_id'";
                    M()->execute($sql);
                    

                    /* 记录订单操作记录 */
                    D('OrderBase')->order_action($order_sn, 1, 0, $pay_status, $note, $user_id);
                }

                   

                   
                 
                elseif ($pay_log['order_type'] == 1) 
                { //预付款
                    $sql = 'SELECT `id` FROM ' . C('DB_PREFIX'). "user_account WHERE `id` = '$pay_log[order_id]' AND `is_paid` = 1  LIMIT 1";
                    $list =M()->query($sql);
                    $res = $list[0];
                    $res_id = $res['id'];
                    if (empty($res_id)) {
                        /* 更新会员预付款的到款状态 */
                        $sql = 'UPDATE ' . C('DB_PREFIX').
                                "user_account SET paid_time = '" . gmtime() . "', is_paid = 1" .
                                " WHERE id = '$pay_log[order_id]' LIMIT 1";
                         M()->execute($sql);

                        /* 取得添加预付款的用户以及金额 */
                        $sql = "SELECT user_id, amount FROM " . C('DB_PREFIX') .
                                "user_account WHERE id = '$pay_log[order_id]'";
                        $list =M()->query($sql);
                        $arr = $list[0];

                        /* 修改会员帐户金额 */
                        D('ClipsBase')->log_account_change($arr['user_id'], $arr['amount'], 0, 0, 0, 'app端支付，会员预付款的到款');
                    }
                }
              }
            } 
            else {
                /* 取得已发货的虚拟商品信息 */


                    /* 记录订单操作记录 */
                      
                /* 有已发货的虚拟商品 */

                //记录错误信息
                $note="pay_log,".$log_id.'支付 有误';
                D('OrderBase')->add_error($note);
               
          }
    }




}

?>
